import com.sun.xml.internal.bind.v2.model.core.ID;
import org.omg.CORBA.INTERNAL;
import util.JdbcUtils;
import util.JdbcUtils_Dbcp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class cats {
    public static void main(String[] args) {
        Insert();
        Delete();
        Update();
        Select();
    }
    //增加数据输入格式为“id,姓名,体重,生日（YYYY-MM-DD）”
    public static void Insert() {

        Connection con = null;
        PreparedStatement st = null;
        ResultSet rs = null;

        System.out.println("请输入猫的ID，姓名，体重及出生年月:");

        Scanner scanner = new Scanner(System.in);
        System.out.println("ID:");
        int id = scanner.nextInt();
        System.out.println("姓名:");
        String name = scanner.next();
        System.out.println("体重：");
        int weight = scanner.nextInt();
        System.out.println("出生年月：");
        String birthday = scanner.next();

        try {
            con = JdbcUtils_Dbcp.getConnection();
            String sql = "Insert INTO cats VALUES(?,?,?,?)";
            st = con.prepareStatement(sql);
            st.setInt(1, id);
            st.setString(2, name);
            st.setInt(3, weight);
            st.setString(4, birthday);

            int i = st.executeUpdate();
            if ( i >= 0 ) {
                System.out.println("新建成功！");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JdbcUtils_Dbcp.release(con, st,rs);
        }

    }

    //输入姓名删除数据
    public static void Delete() {

        Connection con = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        int id;

        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入要删除的猫的ID：");

        id = scanner.nextInt();

        try {
            con = JdbcUtils_Dbcp.getConnection();
            String sql = "DELETE FROM cats WHERE  ID = ?";
            st = con.prepareStatement(sql);
            st.setInt(1, id);

            int i = st.executeUpdate();
            if ( i > 0 ) {
                System.out.println("删除成功！");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JdbcUtils_Dbcp.release(con, st,rs);
        }

    }

    public static void Update() {

        Connection con = null;
        PreparedStatement st = null;
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入想要修改的猫的编号：");
        int id = scanner.nextInt();
        System.out.println("修改后的姓名为：");
        String name = scanner.next();
        try {
            con = JdbcUtils.getConnection();

            String sql = "UPDATE cats SET `NAME` = '" + name + "' WHERE `id` = ? ";
            st = con.prepareStatement(sql);
            st.setInt( 1, id);

            int i = st.executeUpdate();
            if (i > 0) {
                System.out.println("修改成功！");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

    //请输入想要查询的猫猫姓名
    public static void Select() {

        Connection con = null;
        PreparedStatement st = null;
        ResultSet rs = null;

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要查询的猫的ID：");
        int id= scanner.nextInt();
        try {
            con = JdbcUtils.getConnection();
            String sql = "SELECT * FROM cats WHERE ID = ?";
            st = con.prepareStatement(sql);
            st.setInt( 1, id);
            rs = st.executeQuery();

            while (rs.next()) {
                System.out.println("猫猫的ID是：" + rs.getInt("id"));
                System.out.println("猫猫的姓名是：" + rs.getString("name"));
                System.out.println("猫猫的体重是：" + rs.getInt("weight"));
                System.out.println("猫猫的生日是：" + rs.getDate("birthday"));
                System.out.println("=====================");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JdbcUtils.release( con, st, rs);
        }

    }
}
